package com.iuhao.orange.utils;

import com.iuhao.orange.exception.BaseException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 功能描述:
 * 校验实体类
 *
 * @auther: yanhao
 * @param:
 * @date: 2018/8/27 23:16
 * @Description:
 */
public class ValidatorUtils {


    /**
     * 验证实体类
     *
     * @param bean bean对象
     * @param <T>
     * @throws
     */
    public static <T> void validateModel(T bean) throws BaseException {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(bean);
        for (ConstraintViolation<T> violation : constraintViolations) {
            throw new BaseException(violation.getMessage());
        }
    }
}
